function MyMap(_array, call) {
    return _array.map(call)
}

function Groupby(array, f) {
    let groups = {};
    array.forEach(function (o) {
        let group = JSON.stringify(f(o));
        groups[group] = groups[group] || [];
        groups[group].push(o);
    });
    return groups
    // return Object.keys(groups).map(function (group) {
    //     return groups[group];
    // });
}


function UniqBy(_array, key = 'id') {
    var seen = {};
    return _array.filter(function (item) {
        return seen.hasOwnProperty(item[key]) ? false : (seen[item[key]] = true);
    })
}

function removeDuplicate(_array) {
    var grouparray = Groupby(_array, (item) => {
        return item.id
    })
    var newArray = UniqBy(_array, "id")
    MyMap(newArray, (item, index) => {
        var salesOrderAmount = 0
        var count = 0
        MyMap(grouparray[item.id], item1 => {
            count += item1.salesOrderItems.length;
            MyMap(item1['salesOrderItems'], item2 => {
                salesOrderAmount += item2['quantity'] * item2['unitPrice']
            })
        })
        newArray[index]['salesOrderAmount'] = salesOrderAmount
        newArray[index]['count'] = count
    })


    return newArray
}

var data=require('./data')

var result=removeDuplicate(data)
console.log(result)
// module.exports = removeDuplicate